package com.itheima.socket;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @Class: SocketNioServer
 * @Package com.itheima.socket
 * @Description: Nio server
 * 1、阻塞
 * 2、并发
 * @Company: http://www.itheima.com/
 */

public class SocketNioServer {
    
    public static void main(String[] args) throws Exception {
        //定义通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //绑定地址
        serverSocketChannel.bind(new InetSocketAddress("127.0.0.1", 8080));
        //设置为非阻塞模式
        serverSocketChannel.configureBlocking(false);
        //开启一个选择器
        Selector selector = Selector.open();
        //注册
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        while (true) {
            System.out.println("等待连接，阻塞中.....");
            int count = selector.select();
            if (count != 0) {
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                //遍历集合
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();
                    //客户端已经连接，尚未发送数据
                    if (selectionKey.isAcceptable()) {
                        System.out.println("客户端已经连接，尚未发送数据....");
                        //获取通道
                        ServerSocketChannel ssc = (ServerSocketChannel) selectionKey.channel();
                        SocketChannel socketChannel = ssc.accept();
                        //设置非阻塞
                        socketChannel.configureBlocking(false);
                        //注册到选择器
                        socketChannel.register(selector, SelectionKey.OP_READ);
                        
                    }
                    //任务就绪
                    else if (selectionKey.isReadable()) {
                        System.out.println("客户端成功发送数据");
                        //获取通道
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                        //读取信息
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        int read = socketChannel.read(buffer);
                        while (read > 0) {
                            //切换缓冲区的模式
                            buffer.flip();
                            System.out.println(new String(buffer.array(), 0, read));
                            //清除缓冲区
                            buffer.clear();
                            read = socketChannel.read(buffer);
                        }
                        socketChannel.close();//释放资源
                    }
                    iterator.remove();
                    
                }
            }
            
        }
        
    }
}
